class_name CardStateMachine
extends Node

@export var initial_state: CardState

var current_state: CardState
var states := {}
var trophy_mode: bool = false

func init(card: CardView):
	for child in get_children():
		if child is CardState:
			states[child.state] = child
			child.transition_requested.connect(_on_transition_requested)
			child.card_view = card
	if initial_state:
		initial_state.enter()
		current_state = initial_state


func on_mouse_entered():
	if current_state:
		current_state.on_mouse_entered()


func on_mouse_exited():
	if current_state:
		current_state.on_mouse_exited()


func on_card_select():
	if current_state:
		current_state.on_card_select()


func on_card_release():
	if current_state:
		current_state.on_card_release()


func update_transform(in_hand_rotation, in_hand_position, in_hand_hover_position):
	if current_state:
		current_state.update_transform(in_hand_rotation, in_hand_position, in_hand_hover_position)


func _on_transition_requested(from: CardState, to: CardState.State):
	if from != current_state:
		return
	var new_state: CardState = states[to]
	if not new_state:
		return
	if current_state:
		current_state.exit()
	current_state = new_state
	current_state.enter()
